package modellayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * The class ClerkContainer is to collect objects of the class Clerk.
 * 
 * @author Allan Eriksen, Christian Stidsborg, Andreas Rye, Casper Madsen, Stefan Bols 
 * @version 051213
 */
public class ClerkContainer
{
    private static ClerkContainer instance;
    private ArrayList<Clerk> clerkList;

    /**
     * This is the constucter for ClerkContainer which creates an ArrayList
     */
    private ClerkContainer()
    {
        clerkList = new ArrayList<Clerk>();
        // TEST
        createClerk("Stefan", "Bols", "Hemmeligvej 82", "Aalborg", "9000", "", "");
        createClerk("Casper", "Madsen", "Hemmeligvej 83", "Aalborg", "9000", "", "");
        createClerk("Allan", "Eriksen", "Hemmeligvej 84", "Aalborg", "9000", "", "");
        createClerk("Christian", "Stidsborg", "Hemmeligvej 85", "Aalborg", "9000", "", "");
        createClerk("Andreas", "Ryee", "Hemmeligvej 86", "Aalborg", "9000", "", "");
    }
    
    /**
     * This is a singleton method
     * @return ClerkContainer instance
     */
    public static ClerkContainer getInstance()
    {
        if( instance==null )
        {
            instance = new ClerkContainer();
        }        
        return instance;
    }
    
    /**
     * This method creates an object of the class Clerk and adds it to the ArrayList
     * @param String firstName, String lastName, String addressStreet, String addressCity, String addressZipcode
     */
    public void createClerk(String firstName, String lastName, String addressStreet, String addressCity, String addressZipcode, String phone, String email)
    {
        Clerk c = new Clerk(firstName, lastName, addressStreet, addressCity, addressZipcode, phone, email);
        clerkList.add(c);
    }
    
    /**
     * This method finds a specific clerk in the ArrayList and returns its fields.
     * @param int clerkNo
     * @return all fields in a clerk object
     */
    public void readClerk(String clerkNo)
    {
       Iterator<Clerk> it = clerkList.iterator();
        while( it.hasNext())
        {
            Clerk c = it.next();
            String clerkNumber = c.getClerkNo();
            if(clerkNumber.equals(clerkNo))
            {
                c.getClerkNo();
                c.getFirstName();
                c.getLastName();
                c.getAddressStreet();
                c.getAddressCity();
                c.getAddressZipcode();
            }
        } 
    }
    
    /**
     * This method finds a specific clerk in the ArrayList and deletes it
     * @param int clerkNo     * 
     */
    public void deleteClerk(String clerkNo)
    {
        Iterator<Clerk> it = clerkList.iterator();
        while( it.hasNext())
        {
            Clerk c = it.next();
            String clerkNumber = c.getClerkNo();
            if(clerkNumber.equals(clerkNo))
            {
                it.remove();
            }
        }
    }
    
    /**
     * This method finds a specific clerk in the ArrayList and mutates all its fields
     * @param int clerkNo, String firstName, String lastName, String addressStreet, String addressCity, String addressZipcode
     */
    public void updateClerk(String clerkNo, String firstName, String lastName, String addressStreet, String addressCity, String addressZipcode)
    {
        Iterator<Clerk> it = clerkList.iterator();
        while(it.hasNext()){
            Clerk c = it.next();
            String secondClerkNo = c.getClerkNo();
            if(secondClerkNo.equals(clerkNo))
            {
                c.setFirstName(firstName);
                c.setLastName(lastName);
                c.setAddressStreet(addressStreet);
                c.setAddressCity(addressCity);
                c.setAddressZipcode(addressZipcode);            
            }   
        }        
    }
    
    /**
     * This method finds a specific clerk in the ArrayList and returns it
     * @param int clerkNo
     * @return Clerk c
     */
    public Clerk getClerk(String clerkNo)
    {
        Clerk clerk = null;
        int listSize = clerkList.size();
        boolean found = false;
        
        for(int i = 0; i < listSize && !found; i++)
        {            
            if(clerkNo.equals(clerkList.get(i).getClerkNo()))
            {
                clerk = clerkList.get(i);
                found = true;
            }
        }        
        return clerk;
    }
}
